package ru.susu.algebra.centralunits.alternating.tex.local;

import ru.susu.algebra.centralunits.alternating.MathMethodWithInitializers;
import ru.susu.algebra.jtex.ITexElement;
import ru.susu.algebra.jtex.ITexSubItemsElement;
import ru.susu.algebra.jtex.SimpleTexElementWithCode;
import ru.susu.algebra.jtex.StringTexElement;
import ru.susu.algebra.jtex.TexBeginEndElement;
import ru.susu.algebra.jtex.UnionTexElement;
import ru.susu.algebra.jtex.formula.FormulaTexElement;
import ru.susu.algebra.jtex.formula.GreekSymbols;
import ru.susu.algebra.jtex.formula.MathSymbols;
import ru.susu.algebra.jtex.utils.TexUtils;
import ru.susu.algebra.properties.IPropertySource;

/**
 * Главная лемма о делимости. Дает параметризованное выражение alpha и beta, для которых коэффициенты гамма
 * будут целочисленны, то есть те alpha  и beta, которые дают локальную единицу
 *
 * @author akargapolov
 * @since: 09.10.2010
 */
public class MainDivisibilityLemma extends MathMethodWithInitializers<ITexElement>
{
	public static final String LABEL = "main_divisibility_lemma";

	@Override
	protected ITexElement directRun(IPropertySource ps) throws Exception
	{
		String onemb2 = FormulaTexElement.frac("1 - b", "2");
		String onepb2 = FormulaTexElement.frac("1 + b", "2");
		String alphaM1 = GreekSymbols.ALPHA + " - 1";
		String alphaM1Brackets = TexUtils.inBrackets(alphaM1);
		String doubleSlash = "," + MathSymbols.DOUBLE_INV_SLASH + "\n";
		String lambdaM1Brackets = TexUtils.inBrackets(GreekSymbols.LAMBDA + " - 1");
		String localUnitInCentralUnitsGroup = "$u" + TexUtils.inBrackets(GreekSymbols.LAMBDA) + MathSymbols.IN + "\\Un" +
			TexUtils.inBrackets("Z" + TexUtils.inBrackets("\\Z A_n")) + "$"; //$u_{20}(\lambda)\in\Un(Z(\Z A_{14}))$

	/*	\begin{lemma}\label{dz20}
		Пусть $\lambda=\alpha+\beta\omega_{13}$. Локальная единица
		$u_{20}(\lambda)\in\Un(Z(\Z A_{14}))$ тогда и только тогда, когда
		\begin{equation}
		\left\{\begin{aligned}
		9172800&\text{ делит }\alpha-1,\\
		9172800&\text{ делит }\beta.
		\end{aligned}\right.
		\end{equation} */
		ITexSubItemsElement lemma = TexBeginEndElement.lemma().addSubElement(SimpleTexElementWithCode.label(LABEL));
		String bigA =  onemb2 + MathSymbols.PLUS  + " b" +  GreekSymbols.OMEGA;
		String bigA1 =onepb2 + MathSymbols.MINUS  + " b" +  GreekSymbols.OMEGA;
		lemma.addSubElement(StringTexElement.text(
				"Пусть $" + GreekSymbols.LAMBDA + " = " + GreekSymbols.ALPHA + MathSymbols.PLUS + GreekSymbols.BETA + GreekSymbols.OMEGA + "$ и " +
				"$A = " + bigA + "$. " +
				"Локальная единица\n" +
				localUnitInCentralUnitsGroup + " тогда и только тогда, когда\n")).addSubElement(TexBeginEndElement.equation().addSubElement(StringTexElement.text(
						MathSymbols.LEFT + "\\{")).addSubElement(TexBeginEndElement.aligned().addSubElement(StringTexElement.text(
						GreekSymbols.ALPHA + " = 1 + " + FormulaTexElement.frac("z", "2") + FormulaTexElement.frac("bd+1", "bd") + "t " + doubleSlash +
						GreekSymbols.BETA + " = -" + FormulaTexElement.frac("z", "bd") + "t."))).addSubElement(StringTexElement.text(
						MathSymbols.RIGHT + ".")));

	/* По-другому $u_{20}(\lambda)\in\Un(Z(\Z A_{14}))$ тогда и только тогда, когда
		\begin{equation}
		\lambda\in1+z\Z[\omega_{13}].
		\end{equation}
		\end{lemma} */
		lemma.addSubElement(StringTexElement.text(
				"По-другому " + localUnitInCentralUnitsGroup + " тогда и только тогда, когда\n")).addSubElement(TexBeginEndElement.equation().addSubElement(StringTexElement.text(
				GreekSymbols.LAMBDA + MathSymbols.IN + "1+z\\Z[" + GreekSymbols.OMEGA + "].")));

	/* \begin{proof}
		Нужно, чтобы $\gamma_i$ для всех $i$ были целыми. Из вида двадцатой строчки таблицы характеров следует, что нам будут интересны
		\begin{equation}
		\left\{\begin{aligned}
		\frac{\tr(\lambda-1)}{z_{20}}&=\frac{\tr(\lambda-1)}{2^6\cdot3^2\cdot5^2\cdot7^2\cdot13}=
		\frac{\tr(\lambda-1)}{9172800},\\
		\frac{\tr(\texttt{A}(\lambda-1))}{z_{20}}&=\frac{\tr(\texttt{A}(\lambda-1))}{2^6\cdot3^2\cdot5^2\cdot7^2\cdot13}=
		\frac{\tr(\texttt{A}(\lambda-1))}{9172800},\\
		\frac{\tr(\texttt{*A}(\lambda-1))}{z_{20}}&=\frac{\tr(\texttt{*A}(\lambda-1))}{2^6\cdot3^2\cdot5^2\cdot7^2\cdot13}=
		\frac{\tr(\texttt{*A}(\lambda-1))}{9172800}.
		\end{aligned}\right.
		\end{equation} */
		ITexSubItemsElement proof = TexBeginEndElement.proof();
		proof.addSubElement(StringTexElement.text(
				"Нужно, чтобы $" + GreekSymbols.GAMMA + TexUtils.index("i") + "$ для всех $i$ были целыми. Из вида таблицы характеров следует," +
				"что нам будут интересны")).addSubElement(TexBeginEndElement.equation().addSubElement(StringTexElement.text(
				MathSymbols.LEFT + "\\{")).addSubElement(TexBeginEndElement.aligned().addSubElement(StringTexElement.text(
				FormulaTexElement.frac(MathSymbols.TR + lambdaM1Brackets, "z") + "&=" +
				FormulaTexElement.frac("2" + alphaM1Brackets + MathSymbols.PLUS + GreekSymbols.BETA, "z") + doubleSlash +

				FormulaTexElement.frac(MathSymbols.TR + TexUtils.inBrackets("A" + lambdaM1Brackets), "z") + "&=" +
				FormulaTexElement.frac(alphaM1Brackets + MathSymbols.PLUS + FormulaTexElement.frac("1 + bd", "2") + GreekSymbols.BETA, "z") + doubleSlash +

				FormulaTexElement.frac(MathSymbols.TR + TexUtils.inBrackets("*A" + lambdaM1Brackets), "z") + "&=" +
				FormulaTexElement.frac(alphaM1Brackets + MathSymbols.PLUS + FormulaTexElement.frac("1 - bd", "2") + GreekSymbols.BETA, "z")))).addSubElement(StringTexElement.text(
				MathSymbols.RIGHT + ".")));

		TexBeginEndElement align = TexBeginEndElement.align().setWithoutNumber();
		proof.addSubElement(StringTexElement.text("Таким образом надо, чтобы")).addSubElement(align);

		String line1 = "2" + alphaM1Brackets + MathSymbols.PLUS + GreekSymbols.BETA;
		String line2 = alphaM1Brackets + MathSymbols.PLUS + FormulaTexElement.frac("1 + bd", "2") + GreekSymbols.BETA;
		String line3 = alphaM1Brackets + MathSymbols.PLUS + FormulaTexElement.frac("1 - bd", "2") + GreekSymbols.BETA;

		String linez1 = "z&" + MathSymbols.MID + line1;
		String linez2 = "z&" + MathSymbols.MID + line2;
		String linez3 = "z&" + MathSymbols.MID + line3;

	/* Таким образом надо, чтобы
		\begin{align*}
		\left\{\begin{aligned}
		9172800&\mid 2(\alpha-1)+\beta\\
		9172800&\mid (\alpha-1)-6\beta\\
		9172800&\mid (\alpha-1)+7\beta
		\end{aligned}\right.&\iff */
		align.addSubElement(StringTexElement.text(
				MathSymbols.LEFT + "\\{")).addSubElement(TexBeginEndElement.aligned().addSubElement(StringTexElement.text(
				linez1 + MathSymbols.DOUBLE_INV_SLASH + "\n" +
				linez2 + MathSymbols.DOUBLE_INV_SLASH + "\n" +
				linez3))).addSubElement(StringTexElement.text(
						MathSymbols.RIGHT + ".&" + MathSymbols.IFF));

	/* \left\{\begin{aligned}
		9172800&\mid 2(\alpha-1)+\beta\\
		9172800&\mid (\alpha-1)-6\beta\\
		9172800&\mid (\alpha-1)+7\beta
		\end{aligned}\right.\iff\\ */
		align.addSubElement(StringTexElement.text(
				MathSymbols.LEFT + "\\{")).addSubElement(TexBeginEndElement.aligned().addSubElement(StringTexElement.text(
				linez1 + MathSymbols.DOUBLE_INV_SLASH + "\n" +
				linez2))).addSubElement(StringTexElement.text(
						MathSymbols.RIGHT + ".&" + MathSymbols.IFF + MathSymbols.DOUBLE_INV_SLASH + "\n"));

	/* &\iff\left\{\begin{aligned}
		9172800&\mid 2(\alpha-1)+\beta\\
		9172800&\mid (\alpha-1)-6\beta\\
		9172800&\mid (\alpha-1)+7\beta\\
		9172800&\mid -13\beta
		\end{aligned}\right.\iff\\ */
		align.addSubElement(StringTexElement.text(
				"&" + MathSymbols.IFF + MathSymbols.LEFT + "\\{")).addSubElement(TexBeginEndElement.aligned().addSubElement(StringTexElement.text(
				line1 + "+zv_1=0" + MathSymbols.DOUBLE_INV_SLASH + "\n" +
				line2 + "+zv_2=0"))).addSubElement(StringTexElement.text(
						MathSymbols.RIGHT + ".&" + MathSymbols.IFF + MathSymbols.DOUBLE_INV_SLASH + "\n"));

		String one2 = FormulaTexElement.frac("1", "2");
		String z2 = FormulaTexElement.frac("z", "2");
		String zbd = FormulaTexElement.frac("z", "bd");
	/* &\iff\left\{\begin{aligned}
		9172800&\mid bd(\alpha-1)+beta3\beta\\
		9172800&\mid (\alpha-1)-6\beta\\
		9172800&\mid (\alpha-1)+7\beta\\
		9172800&\mid -13\beta
		\end{aligned}\right.\iff\\ */
		align.addSubElement(StringTexElement.text(
				"&" + MathSymbols.IFF + MathSymbols.LEFT + "\\{")).addSubElement(TexBeginEndElement.aligned().addSubElement(StringTexElement.text(
				alphaM1Brackets + "=-" + one2 + GreekSymbols.BETA + MathSymbols.MINUS + z2 + "v_1" + MathSymbols.DOUBLE_INV_SLASH + "\n" +
				"-" + one2 + GreekSymbols.BETA + MathSymbols.MINUS + z2 + "v_1" +
					MathSymbols.PLUS + FormulaTexElement.frac("1 + bd", "2") + GreekSymbols.BETA + "+zv_2=0"))).addSubElement(StringTexElement.text(
				MathSymbols.RIGHT + ".&" + MathSymbols.IFF + MathSymbols.DOUBLE_INV_SLASH + "\n"));

	/* &\iff\left\{\begin{aligned}
		9172800&\mid bd(\alpha-1)\\
		9172800&\mid (\alpha-1)-6\beta\\
		9172800&\mid (\alpha-1)+7\beta\\
		9172800&\mid -13\beta
		\end{aligned}\right.\iff\\ */
		align.addSubElement(StringTexElement.text(
				"&" + MathSymbols.IFF + MathSymbols.LEFT + "\\{")).addSubElement(TexBeginEndElement.aligned().addSubElement(StringTexElement.text(
				alphaM1Brackets + "=-" + one2 + GreekSymbols.BETA + MathSymbols.MINUS + z2 + "v_1" + MathSymbols.DOUBLE_INV_SLASH + "\n" +
				GreekSymbols.BETA + " = " + zbd + "v_1 - 2" + zbd + "v_2"
				))).addSubElement(StringTexElement.text(
						MathSymbols.RIGHT + ".&" + MathSymbols.IFF + MathSymbols.DOUBLE_INV_SLASH + "\n"));

		align.addSubElement(StringTexElement.text(
				"&" + MathSymbols.IFF + MathSymbols.LEFT + "\\{")).addSubElement(TexBeginEndElement.aligned().addSubElement(StringTexElement.text(
				alphaM1Brackets + "=" + one2 + zbd + TexUtils.inBrackets("2v_2-v_1") + MathSymbols.MINUS + z2 + "v_1" + MathSymbols.DOUBLE_INV_SLASH + "\n" +
				GreekSymbols.BETA + " =  -" + zbd + TexUtils.inBrackets("2v_2-v_1")
				))).addSubElement(StringTexElement.text(MathSymbols.RIGHT + "."));

		align = TexBeginEndElement.align().setWithoutNumber();
		proof.addSubElement(StringTexElement.text("Так как интересует делимость на $z$, то можно упростить решение")).addSubElement(align);

		align.addSubElement(StringTexElement.text(
				MathSymbols.LEFT + "\\{")).addSubElement(TexBeginEndElement.aligned().addSubElement(StringTexElement.text(
				alphaM1Brackets + "=" + one2 + zbd + TexUtils.inBrackets("2v_2-v_1") + MathSymbols.MINUS + z2 + "v_1 + zv_2" + MathSymbols.DOUBLE_INV_SLASH + "\n" +
				GreekSymbols.BETA + " =  -" + zbd + TexUtils.inBrackets("2v_2-v_1")))).addSubElement(StringTexElement.text(
						MathSymbols.RIGHT + ".&" + MathSymbols.IFF));

		align.addSubElement(StringTexElement.text(
				MathSymbols.LEFT + "\\{")).addSubElement(TexBeginEndElement.aligned().addSubElement(StringTexElement.text(
				GreekSymbols.ALPHA + "= 1 + " + one2 + zbd +"t" + MathSymbols.PLUS + z2 + "t"  + MathSymbols.DOUBLE_INV_SLASH + "\n" +
				GreekSymbols.BETA + " =  -" + zbd + "t"))).addSubElement(StringTexElement.text(
						MathSymbols.RIGHT + ".&" + MathSymbols.IFF + MathSymbols.DOUBLE_INV_SLASH + "\n"));

		align.addSubElement(StringTexElement.text(
				"&" + MathSymbols.IFF + MathSymbols.LEFT + "\\{")).addSubElement(TexBeginEndElement.aligned().addSubElement(StringTexElement.text(
				GreekSymbols.ALPHA + "=" + "1 + " + z2 + FormulaTexElement.frac("bd + 1", "bd") + "t" + MathSymbols.DOUBLE_INV_SLASH + "\n" +
				GreekSymbols.BETA + " =  -" + zbd + "t"
				))).addSubElement(StringTexElement.text(MathSymbols.RIGHT + "."));

		return new UnionTexElement().addSubElement(StringTexElement.comment(MainDivisibilityLemma.class.getSimpleName()))
			.addSubElement(lemma).addSubElement(proof);
	}

	@Override
	protected Class[] getDependentInitializers()
	{
		return new Class[0];
	}
}
